import 'package:flutter/material.dart';
import 'package:sample2/components/alert_helper.dart';
import 'package:sample2/services/chat_service.dart';
import 'package:sample2/utils/string_util.dart';

class AddChatChannel extends StatefulWidget {
  
  VoidCallback? onSuccessCreateChannel;
  final String adminId;

  AddChatChannel({super.key,required this.adminId,this.onSuccessCreateChannel});

  @override
  State<StatefulWidget> createState() {
    return AddChatChannelState();
  }
}

class AddChatChannelState extends State<AddChatChannel> {

 String? _channelName;
 String? _channelDesc;
 String? _channelTag;

  void _createChannel(){
      
      if(StringUtil.isNullOrEmpty(_channelName)) {
            AlertHelper.showAlert(
                context: context, message: "Please input the group name");
        return;
      }

        ChatService.createChannel(
            adminId: widget.adminId,
            channelName: _channelName!,
            channelDesc: _channelDesc,
            channelTag: _channelTag
            )
        .then((value) {
      AlertHelper.showAlert(
        context: context,
        message: "Create new channel success",
        onPressed: () {
          Navigator.pop(context);
          widget.onSuccessCreateChannel?.call();
        },
      );
    }).catchError((error) {
      debugPrint("got error $error while create new chat group");
    });


  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Add Chat Channel")),
      body: ListView(
        physics: const AlwaysScrollableScrollPhysics(),
        padding: const EdgeInsets.all(13),
        children: [
          Row(
            children: [
              Container(
                constraints: const BoxConstraints(maxWidth: 100),
                child: const Text("Channel Name:"),
              ),
              const SizedBox(
                width: 8,
              ),
              Expanded(
                child: TextField(
                  onChanged: (value) {
                    _channelName = value;
                  },
                  decoration: const InputDecoration(
                      prefixStyle: TextStyle(color: Colors.black),
                      border: InputBorder.none,
                      hintText: "Please input the channel name"),
                ),
              )
            ],
          ),
          Row(
            children: [
              Container(
                constraints: const BoxConstraints(maxWidth: 150),
                child: const Text(
                  "Channel Description:",
                ),
              ),
              const SizedBox(
                width: 8,
              ),
              Expanded(
                child: TextField(
                  onChanged: (value) {
                    _channelDesc = value;
                  },
                  decoration: const InputDecoration(
                      prefixStyle: TextStyle(color: Colors.black),
                      border: InputBorder.none,
                      hintText: "Please input the channel description"),
                ),
              )
            ],
          ),
          Row(
            children: [
              Container(
                constraints: const BoxConstraints(maxWidth: 150),
                child: const Text(
                  "Channel Tag:",
                ),
              ),
              const SizedBox(
                width: 8,
              ),
              Expanded(
                child: TextField(
                  onChanged: (value) {
                    _channelTag = value;
                  },
                  decoration: const InputDecoration(
                      prefixStyle: TextStyle(color: Colors.black),
                      border: InputBorder.none,
                      hintText: "Please input the channel channel tag"),
                ),
              )
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              TextButton(onPressed: _createChannel, child: const Text("Confirm"))
            ],
          ),
          const Spacer(
            flex: 1,
          )
        ],
      ),
    );
  }
}
